
USE [Dienthoai]
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Product_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Product_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Product_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Products_Product table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Product_Get_List

AS


				
				SELECT
					[Products_Id],
					[Products_Name],
					[Products_Title],
					[Products_Quantity],
					[Products_Price],
					[Products_Unit],
					[Class_Id],
					[Brand_Id],
					[Category_Id],
					[Products_Detail],
					[Products_Warranty],
					[Products_Image],
					[Products_Origin],
					[Products_DescriptionImage],
					[QuantityImport],
					[QuantityCurrent],
					[Products_DateUpdate],
					[IsActive],
					[Ishot],
					[IsNew]
				FROM
					[dbo].[Products_Product]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Product_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Product_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Product_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Products_Product table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Product_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Products_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Products_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Products_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Product]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Products_Id], O.[Products_Name], O.[Products_Title], O.[Products_Quantity], O.[Products_Price], O.[Products_Unit], O.[Class_Id], O.[Brand_Id], O.[Category_Id], O.[Products_Detail], O.[Products_Warranty], O.[Products_Image], O.[Products_Origin], O.[Products_DescriptionImage], O.[QuantityImport], O.[QuantityCurrent], O.[Products_DateUpdate], O.[IsActive], O.[Ishot], O.[IsNew]
				FROM
				    [dbo].[Products_Product] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Products_Id] = PageIndex.[Products_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Product]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Product_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Product_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Product_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Products_Product table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Product_Insert
(

	@ProductsId int    OUTPUT,

	@ProductsName nvarchar (50)  ,

	@ProductsTitle nvarchar (50)  ,

	@ProductsQuantity int   ,

	@ProductsPrice int   ,

	@ProductsUnit nvarchar (50)  ,

	@ClassId int   ,

	@BrandId int   ,

	@CategoryId int   ,

	@ProductsDetail ntext   ,

	@ProductsWarranty nvarchar (150)  ,

	@ProductsImage nvarchar (200)  ,

	@ProductsOrigin nvarchar (100)  ,

	@ProductsDescriptionImage nvarchar (100)  ,

	@QuantityImport int   ,

	@QuantityCurrent int   ,

	@ProductsDateUpdate datetime   ,

	@IsActive bit   ,

	@Ishot bit   ,

	@IsNew bit   
)
AS


				
				INSERT INTO [dbo].[Products_Product]
					(
					[Products_Name]
					,[Products_Title]
					,[Products_Quantity]
					,[Products_Price]
					,[Products_Unit]
					,[Class_Id]
					,[Brand_Id]
					,[Category_Id]
					,[Products_Detail]
					,[Products_Warranty]
					,[Products_Image]
					,[Products_Origin]
					,[Products_DescriptionImage]
					,[QuantityImport]
					,[QuantityCurrent]
					,[Products_DateUpdate]
					,[IsActive]
					,[Ishot]
					,[IsNew]
					)
				VALUES
					(
					@ProductsName
					,@ProductsTitle
					,@ProductsQuantity
					,@ProductsPrice
					,@ProductsUnit
					,@ClassId
					,@BrandId
					,@CategoryId
					,@ProductsDetail
					,@ProductsWarranty
					,@ProductsImage
					,@ProductsOrigin
					,@ProductsDescriptionImage
					,@QuantityImport
					,@QuantityCurrent
					,@ProductsDateUpdate
					,@IsActive
					,@Ishot
					,@IsNew
					)
				
				-- Get the identity value
				SET @ProductsId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Product_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Product_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Product_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Products_Product table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Product_Update
(

	@ProductsId int   ,

	@ProductsName nvarchar (50)  ,

	@ProductsTitle nvarchar (50)  ,

	@ProductsQuantity int   ,

	@ProductsPrice int   ,

	@ProductsUnit nvarchar (50)  ,

	@ClassId int   ,

	@BrandId int   ,

	@CategoryId int   ,

	@ProductsDetail ntext   ,

	@ProductsWarranty nvarchar (150)  ,

	@ProductsImage nvarchar (200)  ,

	@ProductsOrigin nvarchar (100)  ,

	@ProductsDescriptionImage nvarchar (100)  ,

	@QuantityImport int   ,

	@QuantityCurrent int   ,

	@ProductsDateUpdate datetime   ,

	@IsActive bit   ,

	@Ishot bit   ,

	@IsNew bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Products_Product]
				SET
					[Products_Name] = @ProductsName
					,[Products_Title] = @ProductsTitle
					,[Products_Quantity] = @ProductsQuantity
					,[Products_Price] = @ProductsPrice
					,[Products_Unit] = @ProductsUnit
					,[Class_Id] = @ClassId
					,[Brand_Id] = @BrandId
					,[Category_Id] = @CategoryId
					,[Products_Detail] = @ProductsDetail
					,[Products_Warranty] = @ProductsWarranty
					,[Products_Image] = @ProductsImage
					,[Products_Origin] = @ProductsOrigin
					,[Products_DescriptionImage] = @ProductsDescriptionImage
					,[QuantityImport] = @QuantityImport
					,[QuantityCurrent] = @QuantityCurrent
					,[Products_DateUpdate] = @ProductsDateUpdate
					,[IsActive] = @IsActive
					,[Ishot] = @Ishot
					,[IsNew] = @IsNew
				WHERE
[Products_Id] = @ProductsId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Product_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Product_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Product_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Products_Product table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Product_Delete
(

	@ProductsId int   
)
AS


				DELETE FROM [dbo].[Products_Product] WITH (ROWLOCK) 
				WHERE
					[Products_Id] = @ProductsId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Product_GetByProductsId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Product_GetByProductsId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Product_GetByProductsId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Products_Product table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Product_GetByProductsId
(

	@ProductsId int   
)
AS


				SELECT
					[Products_Id],
					[Products_Name],
					[Products_Title],
					[Products_Quantity],
					[Products_Price],
					[Products_Unit],
					[Class_Id],
					[Brand_Id],
					[Category_Id],
					[Products_Detail],
					[Products_Warranty],
					[Products_Image],
					[Products_Origin],
					[Products_DescriptionImage],
					[QuantityImport],
					[QuantityCurrent],
					[Products_DateUpdate],
					[IsActive],
					[Ishot],
					[IsNew]
				FROM
					[dbo].[Products_Product]
				WHERE
					[Products_Id] = @ProductsId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Product_GetByOrdersIdFromOrdersDetails procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Product_GetByOrdersIdFromOrdersDetails') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Product_GetByOrdersIdFromOrdersDetails
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records through a junction table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Product_GetByOrdersIdFromOrdersDetails
(

	@OrdersId int   
)
AS


SELECT dbo.[Products_Product].[Products_Id]
       ,dbo.[Products_Product].[Products_Name]
       ,dbo.[Products_Product].[Products_Title]
       ,dbo.[Products_Product].[Products_Quantity]
       ,dbo.[Products_Product].[Products_Price]
       ,dbo.[Products_Product].[Products_Unit]
       ,dbo.[Products_Product].[Class_Id]
       ,dbo.[Products_Product].[Brand_Id]
       ,dbo.[Products_Product].[Category_Id]
       ,dbo.[Products_Product].[Products_Detail]
       ,dbo.[Products_Product].[Products_Warranty]
       ,dbo.[Products_Product].[Products_Image]
       ,dbo.[Products_Product].[Products_Origin]
       ,dbo.[Products_Product].[Products_DescriptionImage]
       ,dbo.[Products_Product].[QuantityImport]
       ,dbo.[Products_Product].[QuantityCurrent]
       ,dbo.[Products_Product].[Products_DateUpdate]
       ,dbo.[Products_Product].[IsActive]
       ,dbo.[Products_Product].[Ishot]
       ,dbo.[Products_Product].[IsNew]
  FROM dbo.[Products_Product]
 WHERE EXISTS (SELECT 1
                 FROM dbo.[Orders_Details] 
                WHERE dbo.[Orders_Details].[Orders_Id] = @OrdersId
                  AND dbo.[Orders_Details].[Products_Id] = dbo.[Products_Product].[Products_Id]
                  )
				SELECT @@ROWCOUNT			
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Product_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Product_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Product_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Products_Product table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Product_Find
(

	@SearchUsingOR bit   = null ,

	@ProductsId int   = null ,

	@ProductsName nvarchar (50)  = null ,

	@ProductsTitle nvarchar (50)  = null ,

	@ProductsQuantity int   = null ,

	@ProductsPrice int   = null ,

	@ProductsUnit nvarchar (50)  = null ,

	@ClassId int   = null ,

	@BrandId int   = null ,

	@CategoryId int   = null ,

	@ProductsDetail ntext   = null ,

	@ProductsWarranty nvarchar (150)  = null ,

	@ProductsImage nvarchar (200)  = null ,

	@ProductsOrigin nvarchar (100)  = null ,

	@ProductsDescriptionImage nvarchar (100)  = null ,

	@QuantityImport int   = null ,

	@QuantityCurrent int   = null ,

	@ProductsDateUpdate datetime   = null ,

	@IsActive bit   = null ,

	@Ishot bit   = null ,

	@IsNew bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Products_Id]
	, [Products_Name]
	, [Products_Title]
	, [Products_Quantity]
	, [Products_Price]
	, [Products_Unit]
	, [Class_Id]
	, [Brand_Id]
	, [Category_Id]
	, [Products_Detail]
	, [Products_Warranty]
	, [Products_Image]
	, [Products_Origin]
	, [Products_DescriptionImage]
	, [QuantityImport]
	, [QuantityCurrent]
	, [Products_DateUpdate]
	, [IsActive]
	, [Ishot]
	, [IsNew]
    FROM
	[dbo].[Products_Product]
    WHERE 
	 ([Products_Id] = @ProductsId OR @ProductsId IS NULL)
	AND ([Products_Name] = @ProductsName OR @ProductsName IS NULL)
	AND ([Products_Title] = @ProductsTitle OR @ProductsTitle IS NULL)
	AND ([Products_Quantity] = @ProductsQuantity OR @ProductsQuantity IS NULL)
	AND ([Products_Price] = @ProductsPrice OR @ProductsPrice IS NULL)
	AND ([Products_Unit] = @ProductsUnit OR @ProductsUnit IS NULL)
	AND ([Class_Id] = @ClassId OR @ClassId IS NULL)
	AND ([Brand_Id] = @BrandId OR @BrandId IS NULL)
	AND ([Category_Id] = @CategoryId OR @CategoryId IS NULL)
	AND ([Products_Warranty] = @ProductsWarranty OR @ProductsWarranty IS NULL)
	AND ([Products_Image] = @ProductsImage OR @ProductsImage IS NULL)
	AND ([Products_Origin] = @ProductsOrigin OR @ProductsOrigin IS NULL)
	AND ([Products_DescriptionImage] = @ProductsDescriptionImage OR @ProductsDescriptionImage IS NULL)
	AND ([QuantityImport] = @QuantityImport OR @QuantityImport IS NULL)
	AND ([QuantityCurrent] = @QuantityCurrent OR @QuantityCurrent IS NULL)
	AND ([Products_DateUpdate] = @ProductsDateUpdate OR @ProductsDateUpdate IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
	AND ([Ishot] = @Ishot OR @Ishot IS NULL)
	AND ([IsNew] = @IsNew OR @IsNew IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Products_Id]
	, [Products_Name]
	, [Products_Title]
	, [Products_Quantity]
	, [Products_Price]
	, [Products_Unit]
	, [Class_Id]
	, [Brand_Id]
	, [Category_Id]
	, [Products_Detail]
	, [Products_Warranty]
	, [Products_Image]
	, [Products_Origin]
	, [Products_DescriptionImage]
	, [QuantityImport]
	, [QuantityCurrent]
	, [Products_DateUpdate]
	, [IsActive]
	, [Ishot]
	, [IsNew]
    FROM
	[dbo].[Products_Product]
    WHERE 
	 ([Products_Id] = @ProductsId AND @ProductsId is not null)
	OR ([Products_Name] = @ProductsName AND @ProductsName is not null)
	OR ([Products_Title] = @ProductsTitle AND @ProductsTitle is not null)
	OR ([Products_Quantity] = @ProductsQuantity AND @ProductsQuantity is not null)
	OR ([Products_Price] = @ProductsPrice AND @ProductsPrice is not null)
	OR ([Products_Unit] = @ProductsUnit AND @ProductsUnit is not null)
	OR ([Class_Id] = @ClassId AND @ClassId is not null)
	OR ([Brand_Id] = @BrandId AND @BrandId is not null)
	OR ([Category_Id] = @CategoryId AND @CategoryId is not null)
	OR ([Products_Warranty] = @ProductsWarranty AND @ProductsWarranty is not null)
	OR ([Products_Image] = @ProductsImage AND @ProductsImage is not null)
	OR ([Products_Origin] = @ProductsOrigin AND @ProductsOrigin is not null)
	OR ([Products_DescriptionImage] = @ProductsDescriptionImage AND @ProductsDescriptionImage is not null)
	OR ([QuantityImport] = @QuantityImport AND @QuantityImport is not null)
	OR ([QuantityCurrent] = @QuantityCurrent AND @QuantityCurrent is not null)
	OR ([Products_DateUpdate] = @ProductsDateUpdate AND @ProductsDateUpdate is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([Ishot] = @Ishot AND @Ishot is not null)
	OR ([IsNew] = @IsNew AND @IsNew is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Comment_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Comment_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Comment_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Comment table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Comment_Get_List

AS


				
				SELECT
					[Comment_Id],
					[Product_Id],
					[Comment_Name],
					[Comment_Email],
					[Comment_Detail],
					[Comment_Title],
					[Comment_Date]
				FROM
					[dbo].[Comment]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Comment_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Comment_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Comment_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Comment table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Comment_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Comment_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Comment_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Comment_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Comment]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Comment_Id], O.[Product_Id], O.[Comment_Name], O.[Comment_Email], O.[Comment_Detail], O.[Comment_Title], O.[Comment_Date]
				FROM
				    [dbo].[Comment] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Comment_Id] = PageIndex.[Comment_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Comment]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Comment_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Comment_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Comment_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Comment table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Comment_Insert
(

	@CommentId int    OUTPUT,

	@ProductId int   ,

	@CommentName nvarchar (50)  ,

	@CommentEmail nvarchar (50)  ,

	@CommentDetail nvarchar (200)  ,

	@CommentTitle nchar (100)  ,

	@CommentDate datetime   
)
AS


				
				INSERT INTO [dbo].[Comment]
					(
					[Product_Id]
					,[Comment_Name]
					,[Comment_Email]
					,[Comment_Detail]
					,[Comment_Title]
					,[Comment_Date]
					)
				VALUES
					(
					@ProductId
					,@CommentName
					,@CommentEmail
					,@CommentDetail
					,@CommentTitle
					,@CommentDate
					)
				
				-- Get the identity value
				SET @CommentId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Comment_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Comment_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Comment_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Comment table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Comment_Update
(

	@CommentId int   ,

	@ProductId int   ,

	@CommentName nvarchar (50)  ,

	@CommentEmail nvarchar (50)  ,

	@CommentDetail nvarchar (200)  ,

	@CommentTitle nchar (100)  ,

	@CommentDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Comment]
				SET
					[Product_Id] = @ProductId
					,[Comment_Name] = @CommentName
					,[Comment_Email] = @CommentEmail
					,[Comment_Detail] = @CommentDetail
					,[Comment_Title] = @CommentTitle
					,[Comment_Date] = @CommentDate
				WHERE
[Comment_Id] = @CommentId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Comment_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Comment_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Comment_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Comment table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Comment_Delete
(

	@CommentId int   
)
AS


				DELETE FROM [dbo].[Comment] WITH (ROWLOCK) 
				WHERE
					[Comment_Id] = @CommentId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Comment_GetByCommentId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Comment_GetByCommentId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Comment_GetByCommentId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Comment table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Comment_GetByCommentId
(

	@CommentId int   
)
AS


				SELECT
					[Comment_Id],
					[Product_Id],
					[Comment_Name],
					[Comment_Email],
					[Comment_Detail],
					[Comment_Title],
					[Comment_Date]
				FROM
					[dbo].[Comment]
				WHERE
					[Comment_Id] = @CommentId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Comment_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Comment_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Comment_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Comment table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Comment_Find
(

	@SearchUsingOR bit   = null ,

	@CommentId int   = null ,

	@ProductId int   = null ,

	@CommentName nvarchar (50)  = null ,

	@CommentEmail nvarchar (50)  = null ,

	@CommentDetail nvarchar (200)  = null ,

	@CommentTitle nchar (100)  = null ,

	@CommentDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Comment_Id]
	, [Product_Id]
	, [Comment_Name]
	, [Comment_Email]
	, [Comment_Detail]
	, [Comment_Title]
	, [Comment_Date]
    FROM
	[dbo].[Comment]
    WHERE 
	 ([Comment_Id] = @CommentId OR @CommentId IS NULL)
	AND ([Product_Id] = @ProductId OR @ProductId IS NULL)
	AND ([Comment_Name] = @CommentName OR @CommentName IS NULL)
	AND ([Comment_Email] = @CommentEmail OR @CommentEmail IS NULL)
	AND ([Comment_Detail] = @CommentDetail OR @CommentDetail IS NULL)
	AND ([Comment_Title] = @CommentTitle OR @CommentTitle IS NULL)
	AND ([Comment_Date] = @CommentDate OR @CommentDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Comment_Id]
	, [Product_Id]
	, [Comment_Name]
	, [Comment_Email]
	, [Comment_Detail]
	, [Comment_Title]
	, [Comment_Date]
    FROM
	[dbo].[Comment]
    WHERE 
	 ([Comment_Id] = @CommentId AND @CommentId is not null)
	OR ([Product_Id] = @ProductId AND @ProductId is not null)
	OR ([Comment_Name] = @CommentName AND @CommentName is not null)
	OR ([Comment_Email] = @CommentEmail AND @CommentEmail is not null)
	OR ([Comment_Detail] = @CommentDetail AND @CommentDetail is not null)
	OR ([Comment_Title] = @CommentTitle AND @CommentTitle is not null)
	OR ([Comment_Date] = @CommentDate AND @CommentDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Type_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Type_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Type_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Products_Type table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Type_Get_List

AS


				
				SELECT
					[Type_Id],
					[Type_Name],
					[Category_Id],
					[Type_Title],
					[IsActive]
				FROM
					[dbo].[Products_Type]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Type_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Type_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Type_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Products_Type table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Type_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Type_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Type_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Type_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Type]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Type_Id], O.[Type_Name], O.[Category_Id], O.[Type_Title], O.[IsActive]
				FROM
				    [dbo].[Products_Type] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Type_Id] = PageIndex.[Type_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Type]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Type_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Type_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Type_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Products_Type table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Type_Insert
(

	@TypeId int    OUTPUT,

	@TypeName nvarchar (50)  ,

	@CategoryId int   ,

	@TypeTitle nvarchar (50)  ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[Products_Type]
					(
					[Type_Name]
					,[Category_Id]
					,[Type_Title]
					,[IsActive]
					)
				VALUES
					(
					@TypeName
					,@CategoryId
					,@TypeTitle
					,@IsActive
					)
				
				-- Get the identity value
				SET @TypeId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Type_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Type_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Type_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Products_Type table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Type_Update
(

	@TypeId int   ,

	@TypeName nvarchar (50)  ,

	@CategoryId int   ,

	@TypeTitle nvarchar (50)  ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Products_Type]
				SET
					[Type_Name] = @TypeName
					,[Category_Id] = @CategoryId
					,[Type_Title] = @TypeTitle
					,[IsActive] = @IsActive
				WHERE
[Type_Id] = @TypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Type_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Type_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Type_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Products_Type table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Type_Delete
(

	@TypeId int   
)
AS


				DELETE FROM [dbo].[Products_Type] WITH (ROWLOCK) 
				WHERE
					[Type_Id] = @TypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Type_GetByTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Type_GetByTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Type_GetByTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Products_Type table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Type_GetByTypeId
(

	@TypeId int   
)
AS


				SELECT
					[Type_Id],
					[Type_Name],
					[Category_Id],
					[Type_Title],
					[IsActive]
				FROM
					[dbo].[Products_Type]
				WHERE
					[Type_Id] = @TypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Type_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Type_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Type_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Products_Type table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Type_Find
(

	@SearchUsingOR bit   = null ,

	@TypeId int   = null ,

	@TypeName nvarchar (50)  = null ,

	@CategoryId int   = null ,

	@TypeTitle nvarchar (50)  = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Type_Id]
	, [Type_Name]
	, [Category_Id]
	, [Type_Title]
	, [IsActive]
    FROM
	[dbo].[Products_Type]
    WHERE 
	 ([Type_Id] = @TypeId OR @TypeId IS NULL)
	AND ([Type_Name] = @TypeName OR @TypeName IS NULL)
	AND ([Category_Id] = @CategoryId OR @CategoryId IS NULL)
	AND ([Type_Title] = @TypeTitle OR @TypeTitle IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Type_Id]
	, [Type_Name]
	, [Category_Id]
	, [Type_Title]
	, [IsActive]
    FROM
	[dbo].[Products_Type]
    WHERE 
	 ([Type_Id] = @TypeId AND @TypeId is not null)
	OR ([Type_Name] = @TypeName AND @TypeName is not null)
	OR ([Category_Id] = @CategoryId AND @CategoryId is not null)
	OR ([Type_Title] = @TypeTitle AND @TypeTitle is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Class_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Class_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Class_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Products_Class table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Class_Get_List

AS


				
				SELECT
					[Class_Id],
					[Class_Name],
					[Class_Title],
					[Class_Sort],
					[IsActive]
				FROM
					[dbo].[Products_Class]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Class_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Class_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Class_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Products_Class table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Class_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Class_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Class_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Class_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Class]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Class_Id], O.[Class_Name], O.[Class_Title], O.[Class_Sort], O.[IsActive]
				FROM
				    [dbo].[Products_Class] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Class_Id] = PageIndex.[Class_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Class]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Class_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Class_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Class_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Products_Class table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Class_Insert
(

	@ClassId int    OUTPUT,

	@ClassName nvarchar (50)  ,

	@ClassTitle nvarchar (50)  ,

	@ClassSort int   ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[Products_Class]
					(
					[Class_Name]
					,[Class_Title]
					,[Class_Sort]
					,[IsActive]
					)
				VALUES
					(
					@ClassName
					,@ClassTitle
					,@ClassSort
					,@IsActive
					)
				
				-- Get the identity value
				SET @ClassId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Class_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Class_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Class_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Products_Class table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Class_Update
(

	@ClassId int   ,

	@ClassName nvarchar (50)  ,

	@ClassTitle nvarchar (50)  ,

	@ClassSort int   ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Products_Class]
				SET
					[Class_Name] = @ClassName
					,[Class_Title] = @ClassTitle
					,[Class_Sort] = @ClassSort
					,[IsActive] = @IsActive
				WHERE
[Class_Id] = @ClassId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Class_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Class_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Class_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Products_Class table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Class_Delete
(

	@ClassId int   
)
AS


				DELETE FROM [dbo].[Products_Class] WITH (ROWLOCK) 
				WHERE
					[Class_Id] = @ClassId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Class_GetByClassId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Class_GetByClassId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Class_GetByClassId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Products_Class table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Class_GetByClassId
(

	@ClassId int   
)
AS


				SELECT
					[Class_Id],
					[Class_Name],
					[Class_Title],
					[Class_Sort],
					[IsActive]
				FROM
					[dbo].[Products_Class]
				WHERE
					[Class_Id] = @ClassId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Class_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Class_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Class_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Products_Class table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Class_Find
(

	@SearchUsingOR bit   = null ,

	@ClassId int   = null ,

	@ClassName nvarchar (50)  = null ,

	@ClassTitle nvarchar (50)  = null ,

	@ClassSort int   = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Class_Id]
	, [Class_Name]
	, [Class_Title]
	, [Class_Sort]
	, [IsActive]
    FROM
	[dbo].[Products_Class]
    WHERE 
	 ([Class_Id] = @ClassId OR @ClassId IS NULL)
	AND ([Class_Name] = @ClassName OR @ClassName IS NULL)
	AND ([Class_Title] = @ClassTitle OR @ClassTitle IS NULL)
	AND ([Class_Sort] = @ClassSort OR @ClassSort IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Class_Id]
	, [Class_Name]
	, [Class_Title]
	, [Class_Sort]
	, [IsActive]
    FROM
	[dbo].[Products_Class]
    WHERE 
	 ([Class_Id] = @ClassId AND @ClassId is not null)
	OR ([Class_Name] = @ClassName AND @ClassName is not null)
	OR ([Class_Title] = @ClassTitle AND @ClassTitle is not null)
	OR ([Class_Sort] = @ClassSort AND @ClassSort is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Members_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Members_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Members_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Members table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Members_Get_List

AS


				
				SELECT
					[Members_Id],
					[Members_Name],
					[Members_Login],
					[Members_Password],
					[Members_Address],
					[Members_Tel],
					[Members_DateJoin],
					[Members_Mail],
					[Members_LastLogin],
					[Members_City],
					[IsActive]
				FROM
					[dbo].[Members]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Members_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Members_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Members_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Members table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Members_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Members_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Members_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Members_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Members]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Members_Id], O.[Members_Name], O.[Members_Login], O.[Members_Password], O.[Members_Address], O.[Members_Tel], O.[Members_DateJoin], O.[Members_Mail], O.[Members_LastLogin], O.[Members_City], O.[IsActive]
				FROM
				    [dbo].[Members] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Members_Id] = PageIndex.[Members_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Members]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Members_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Members_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Members_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Members table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Members_Insert
(

	@MembersId int    OUTPUT,

	@MembersName nvarchar (100)  ,

	@MembersLogin nvarchar (50)  ,

	@MembersPassword binary (500)  ,

	@MembersAddress nvarchar (200)  ,

	@MembersTel nvarchar (15)  ,

	@MembersDateJoin smalldatetime   ,

	@MembersMail nvarchar (50)  ,

	@MembersLastLogin smalldatetime   ,

	@MembersCity nchar (10)  ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[Members]
					(
					[Members_Name]
					,[Members_Login]
					,[Members_Password]
					,[Members_Address]
					,[Members_Tel]
					,[Members_DateJoin]
					,[Members_Mail]
					,[Members_LastLogin]
					,[Members_City]
					,[IsActive]
					)
				VALUES
					(
					@MembersName
					,@MembersLogin
					,@MembersPassword
					,@MembersAddress
					,@MembersTel
					,@MembersDateJoin
					,@MembersMail
					,@MembersLastLogin
					,@MembersCity
					,@IsActive
					)
				
				-- Get the identity value
				SET @MembersId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Members_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Members_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Members_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Members table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Members_Update
(

	@MembersId int   ,

	@MembersName nvarchar (100)  ,

	@MembersLogin nvarchar (50)  ,

	@MembersPassword binary (500)  ,

	@MembersAddress nvarchar (200)  ,

	@MembersTel nvarchar (15)  ,

	@MembersDateJoin smalldatetime   ,

	@MembersMail nvarchar (50)  ,

	@MembersLastLogin smalldatetime   ,

	@MembersCity nchar (10)  ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Members]
				SET
					[Members_Name] = @MembersName
					,[Members_Login] = @MembersLogin
					,[Members_Password] = @MembersPassword
					,[Members_Address] = @MembersAddress
					,[Members_Tel] = @MembersTel
					,[Members_DateJoin] = @MembersDateJoin
					,[Members_Mail] = @MembersMail
					,[Members_LastLogin] = @MembersLastLogin
					,[Members_City] = @MembersCity
					,[IsActive] = @IsActive
				WHERE
[Members_Id] = @MembersId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Members_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Members_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Members_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Members table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Members_Delete
(

	@MembersId int   
)
AS


				DELETE FROM [dbo].[Members] WITH (ROWLOCK) 
				WHERE
					[Members_Id] = @MembersId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Members_GetByMembersId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Members_GetByMembersId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Members_GetByMembersId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Members table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Members_GetByMembersId
(

	@MembersId int   
)
AS


				SELECT
					[Members_Id],
					[Members_Name],
					[Members_Login],
					[Members_Password],
					[Members_Address],
					[Members_Tel],
					[Members_DateJoin],
					[Members_Mail],
					[Members_LastLogin],
					[Members_City],
					[IsActive]
				FROM
					[dbo].[Members]
				WHERE
					[Members_Id] = @MembersId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Members_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Members_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Members_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Members table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Members_Find
(

	@SearchUsingOR bit   = null ,

	@MembersId int   = null ,

	@MembersName nvarchar (100)  = null ,

	@MembersLogin nvarchar (50)  = null ,

	@MembersPassword binary (500)  = null ,

	@MembersAddress nvarchar (200)  = null ,

	@MembersTel nvarchar (15)  = null ,

	@MembersDateJoin smalldatetime   = null ,

	@MembersMail nvarchar (50)  = null ,

	@MembersLastLogin smalldatetime   = null ,

	@MembersCity nchar (10)  = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Members_Id]
	, [Members_Name]
	, [Members_Login]
	, [Members_Password]
	, [Members_Address]
	, [Members_Tel]
	, [Members_DateJoin]
	, [Members_Mail]
	, [Members_LastLogin]
	, [Members_City]
	, [IsActive]
    FROM
	[dbo].[Members]
    WHERE 
	 ([Members_Id] = @MembersId OR @MembersId IS NULL)
	AND ([Members_Name] = @MembersName OR @MembersName IS NULL)
	AND ([Members_Login] = @MembersLogin OR @MembersLogin IS NULL)
	AND ([Members_Address] = @MembersAddress OR @MembersAddress IS NULL)
	AND ([Members_Tel] = @MembersTel OR @MembersTel IS NULL)
	AND ([Members_DateJoin] = @MembersDateJoin OR @MembersDateJoin IS NULL)
	AND ([Members_Mail] = @MembersMail OR @MembersMail IS NULL)
	AND ([Members_LastLogin] = @MembersLastLogin OR @MembersLastLogin IS NULL)
	AND ([Members_City] = @MembersCity OR @MembersCity IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Members_Id]
	, [Members_Name]
	, [Members_Login]
	, [Members_Password]
	, [Members_Address]
	, [Members_Tel]
	, [Members_DateJoin]
	, [Members_Mail]
	, [Members_LastLogin]
	, [Members_City]
	, [IsActive]
    FROM
	[dbo].[Members]
    WHERE 
	 ([Members_Id] = @MembersId AND @MembersId is not null)
	OR ([Members_Name] = @MembersName AND @MembersName is not null)
	OR ([Members_Login] = @MembersLogin AND @MembersLogin is not null)
	OR ([Members_Address] = @MembersAddress AND @MembersAddress is not null)
	OR ([Members_Tel] = @MembersTel AND @MembersTel is not null)
	OR ([Members_DateJoin] = @MembersDateJoin AND @MembersDateJoin is not null)
	OR ([Members_Mail] = @MembersMail AND @MembersMail is not null)
	OR ([Members_LastLogin] = @MembersLastLogin AND @MembersLastLogin is not null)
	OR ([Members_City] = @MembersCity AND @MembersCity is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Supply_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Supply_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Supply_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Supply table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Supply_Get_List

AS


				
				SELECT
					[Supply_Id],
					[Supply_Name],
					[Supply_Adress],
					[Supply_Tel],
					[Sypply_Fax],
					[Sypply_Mail],
					[Notice]
				FROM
					[dbo].[Supply]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Supply_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Supply_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Supply_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Supply table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Supply_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Supply_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Supply_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Supply_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Supply]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Supply_Id], O.[Supply_Name], O.[Supply_Adress], O.[Supply_Tel], O.[Sypply_Fax], O.[Sypply_Mail], O.[Notice]
				FROM
				    [dbo].[Supply] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Supply_Id] = PageIndex.[Supply_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Supply]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Supply_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Supply_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Supply_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Supply table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Supply_Insert
(

	@SupplyId int    OUTPUT,

	@SupplyName nvarchar (100)  ,

	@SupplyAdress nvarchar (250)  ,

	@SupplyTel nchar (12)  ,

	@SypplyFax nchar (12)  ,

	@SypplyMail nvarchar (100)  ,

	@Notice nvarchar (250)  
)
AS


				
				INSERT INTO [dbo].[Supply]
					(
					[Supply_Name]
					,[Supply_Adress]
					,[Supply_Tel]
					,[Sypply_Fax]
					,[Sypply_Mail]
					,[Notice]
					)
				VALUES
					(
					@SupplyName
					,@SupplyAdress
					,@SupplyTel
					,@SypplyFax
					,@SypplyMail
					,@Notice
					)
				
				-- Get the identity value
				SET @SupplyId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Supply_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Supply_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Supply_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Supply table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Supply_Update
(

	@SupplyId int   ,

	@SupplyName nvarchar (100)  ,

	@SupplyAdress nvarchar (250)  ,

	@SupplyTel nchar (12)  ,

	@SypplyFax nchar (12)  ,

	@SypplyMail nvarchar (100)  ,

	@Notice nvarchar (250)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Supply]
				SET
					[Supply_Name] = @SupplyName
					,[Supply_Adress] = @SupplyAdress
					,[Supply_Tel] = @SupplyTel
					,[Sypply_Fax] = @SypplyFax
					,[Sypply_Mail] = @SypplyMail
					,[Notice] = @Notice
				WHERE
[Supply_Id] = @SupplyId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Supply_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Supply_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Supply_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Supply table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Supply_Delete
(

	@SupplyId int   
)
AS


				DELETE FROM [dbo].[Supply] WITH (ROWLOCK) 
				WHERE
					[Supply_Id] = @SupplyId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Supply_GetBySupplyId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Supply_GetBySupplyId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Supply_GetBySupplyId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Supply table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Supply_GetBySupplyId
(

	@SupplyId int   
)
AS


				SELECT
					[Supply_Id],
					[Supply_Name],
					[Supply_Adress],
					[Supply_Tel],
					[Sypply_Fax],
					[Sypply_Mail],
					[Notice]
				FROM
					[dbo].[Supply]
				WHERE
					[Supply_Id] = @SupplyId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Supply_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Supply_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Supply_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Supply table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Supply_Find
(

	@SearchUsingOR bit   = null ,

	@SupplyId int   = null ,

	@SupplyName nvarchar (100)  = null ,

	@SupplyAdress nvarchar (250)  = null ,

	@SupplyTel nchar (12)  = null ,

	@SypplyFax nchar (12)  = null ,

	@SypplyMail nvarchar (100)  = null ,

	@Notice nvarchar (250)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Supply_Id]
	, [Supply_Name]
	, [Supply_Adress]
	, [Supply_Tel]
	, [Sypply_Fax]
	, [Sypply_Mail]
	, [Notice]
    FROM
	[dbo].[Supply]
    WHERE 
	 ([Supply_Id] = @SupplyId OR @SupplyId IS NULL)
	AND ([Supply_Name] = @SupplyName OR @SupplyName IS NULL)
	AND ([Supply_Adress] = @SupplyAdress OR @SupplyAdress IS NULL)
	AND ([Supply_Tel] = @SupplyTel OR @SupplyTel IS NULL)
	AND ([Sypply_Fax] = @SypplyFax OR @SypplyFax IS NULL)
	AND ([Sypply_Mail] = @SypplyMail OR @SypplyMail IS NULL)
	AND ([Notice] = @Notice OR @Notice IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Supply_Id]
	, [Supply_Name]
	, [Supply_Adress]
	, [Supply_Tel]
	, [Sypply_Fax]
	, [Sypply_Mail]
	, [Notice]
    FROM
	[dbo].[Supply]
    WHERE 
	 ([Supply_Id] = @SupplyId AND @SupplyId is not null)
	OR ([Supply_Name] = @SupplyName AND @SupplyName is not null)
	OR ([Supply_Adress] = @SupplyAdress AND @SupplyAdress is not null)
	OR ([Supply_Tel] = @SupplyTel AND @SupplyTel is not null)
	OR ([Sypply_Fax] = @SypplyFax AND @SypplyFax is not null)
	OR ([Sypply_Mail] = @SypplyMail AND @SypplyMail is not null)
	OR ([Notice] = @Notice AND @Notice is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUser_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUser_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUser_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the TblUser table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUser_Get_List

AS


				
				SELECT
					[User_Id],
					[User_FullName],
					[User_NameLogin],
					[User_Password],
					[User_LastLogin],
					[User_Function],
					[IsActive]
				FROM
					[dbo].[TblUser]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUser_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUser_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUser_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the TblUser table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUser_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [User_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([User_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [User_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[TblUser]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[User_Id], O.[User_FullName], O.[User_NameLogin], O.[User_Password], O.[User_LastLogin], O.[User_Function], O.[IsActive]
				FROM
				    [dbo].[TblUser] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[User_Id] = PageIndex.[User_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[TblUser]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUser_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUser_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUser_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the TblUser table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUser_Insert
(

	@UserId int    OUTPUT,

	@UserFullName nvarchar (50)  ,

	@UserNameLogin nvarchar (50)  ,

	@UserPassword binary (500)  ,

	@UserLastLogin nvarchar (50)  ,

	@UserFunction bit   ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[TblUser]
					(
					[User_FullName]
					,[User_NameLogin]
					,[User_Password]
					,[User_LastLogin]
					,[User_Function]
					,[IsActive]
					)
				VALUES
					(
					@UserFullName
					,@UserNameLogin
					,@UserPassword
					,@UserLastLogin
					,@UserFunction
					,@IsActive
					)
				
				-- Get the identity value
				SET @UserId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUser_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUser_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUser_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the TblUser table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUser_Update
(

	@UserId int   ,

	@UserFullName nvarchar (50)  ,

	@UserNameLogin nvarchar (50)  ,

	@UserPassword binary (500)  ,

	@UserLastLogin nvarchar (50)  ,

	@UserFunction bit   ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[TblUser]
				SET
					[User_FullName] = @UserFullName
					,[User_NameLogin] = @UserNameLogin
					,[User_Password] = @UserPassword
					,[User_LastLogin] = @UserLastLogin
					,[User_Function] = @UserFunction
					,[IsActive] = @IsActive
				WHERE
[User_Id] = @UserId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUser_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUser_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUser_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the TblUser table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUser_Delete
(

	@UserId int   
)
AS


				DELETE FROM [dbo].[TblUser] WITH (ROWLOCK) 
				WHERE
					[User_Id] = @UserId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUser_GetByUserId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUser_GetByUserId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUser_GetByUserId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the TblUser table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUser_GetByUserId
(

	@UserId int   
)
AS


				SELECT
					[User_Id],
					[User_FullName],
					[User_NameLogin],
					[User_Password],
					[User_LastLogin],
					[User_Function],
					[IsActive]
				FROM
					[dbo].[TblUser]
				WHERE
					[User_Id] = @UserId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUser_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUser_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUser_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the TblUser table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUser_Find
(

	@SearchUsingOR bit   = null ,

	@UserId int   = null ,

	@UserFullName nvarchar (50)  = null ,

	@UserNameLogin nvarchar (50)  = null ,

	@UserPassword binary (500)  = null ,

	@UserLastLogin nvarchar (50)  = null ,

	@UserFunction bit   = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [User_Id]
	, [User_FullName]
	, [User_NameLogin]
	, [User_Password]
	, [User_LastLogin]
	, [User_Function]
	, [IsActive]
    FROM
	[dbo].[TblUser]
    WHERE 
	 ([User_Id] = @UserId OR @UserId IS NULL)
	AND ([User_FullName] = @UserFullName OR @UserFullName IS NULL)
	AND ([User_NameLogin] = @UserNameLogin OR @UserNameLogin IS NULL)
	AND ([User_LastLogin] = @UserLastLogin OR @UserLastLogin IS NULL)
	AND ([User_Function] = @UserFunction OR @UserFunction IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [User_Id]
	, [User_FullName]
	, [User_NameLogin]
	, [User_Password]
	, [User_LastLogin]
	, [User_Function]
	, [IsActive]
    FROM
	[dbo].[TblUser]
    WHERE 
	 ([User_Id] = @UserId AND @UserId is not null)
	OR ([User_FullName] = @UserFullName AND @UserFullName is not null)
	OR ([User_NameLogin] = @UserNameLogin AND @UserNameLogin is not null)
	OR ([User_LastLogin] = @UserLastLogin AND @UserLastLogin is not null)
	OR ([User_Function] = @UserFunction AND @UserFunction is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Category_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Category_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Category_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Products_Category table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Category_Get_List

AS


				
				SELECT
					[Category_Id],
					[Category_Name],
					[Category_Title],
					[Category_sort],
					[IsActive]
				FROM
					[dbo].[Products_Category]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Category_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Category_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Category_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Products_Category table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Category_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Category_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Category_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Category_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Category]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Category_Id], O.[Category_Name], O.[Category_Title], O.[Category_sort], O.[IsActive]
				FROM
				    [dbo].[Products_Category] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Category_Id] = PageIndex.[Category_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Category]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Category_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Category_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Category_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Products_Category table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Category_Insert
(

	@CategoryId int    OUTPUT,

	@CategoryName nvarchar (50)  ,

	@CategoryTitle nvarchar (50)  ,

	@CategorySort int   ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[Products_Category]
					(
					[Category_Name]
					,[Category_Title]
					,[Category_sort]
					,[IsActive]
					)
				VALUES
					(
					@CategoryName
					,@CategoryTitle
					,@CategorySort
					,@IsActive
					)
				
				-- Get the identity value
				SET @CategoryId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Category_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Category_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Category_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Products_Category table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Category_Update
(

	@CategoryId int   ,

	@CategoryName nvarchar (50)  ,

	@CategoryTitle nvarchar (50)  ,

	@CategorySort int   ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Products_Category]
				SET
					[Category_Name] = @CategoryName
					,[Category_Title] = @CategoryTitle
					,[Category_sort] = @CategorySort
					,[IsActive] = @IsActive
				WHERE
[Category_Id] = @CategoryId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Category_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Category_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Category_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Products_Category table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Category_Delete
(

	@CategoryId int   
)
AS


				DELETE FROM [dbo].[Products_Category] WITH (ROWLOCK) 
				WHERE
					[Category_Id] = @CategoryId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Category_GetByCategoryId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Category_GetByCategoryId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Category_GetByCategoryId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Products_Category table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Category_GetByCategoryId
(

	@CategoryId int   
)
AS


				SELECT
					[Category_Id],
					[Category_Name],
					[Category_Title],
					[Category_sort],
					[IsActive]
				FROM
					[dbo].[Products_Category]
				WHERE
					[Category_Id] = @CategoryId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Category_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Category_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Category_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Products_Category table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Category_Find
(

	@SearchUsingOR bit   = null ,

	@CategoryId int   = null ,

	@CategoryName nvarchar (50)  = null ,

	@CategoryTitle nvarchar (50)  = null ,

	@CategorySort int   = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Category_Id]
	, [Category_Name]
	, [Category_Title]
	, [Category_sort]
	, [IsActive]
    FROM
	[dbo].[Products_Category]
    WHERE 
	 ([Category_Id] = @CategoryId OR @CategoryId IS NULL)
	AND ([Category_Name] = @CategoryName OR @CategoryName IS NULL)
	AND ([Category_Title] = @CategoryTitle OR @CategoryTitle IS NULL)
	AND ([Category_sort] = @CategorySort OR @CategorySort IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Category_Id]
	, [Category_Name]
	, [Category_Title]
	, [Category_sort]
	, [IsActive]
    FROM
	[dbo].[Products_Category]
    WHERE 
	 ([Category_Id] = @CategoryId AND @CategoryId is not null)
	OR ([Category_Name] = @CategoryName AND @CategoryName is not null)
	OR ([Category_Title] = @CategoryTitle AND @CategoryTitle is not null)
	OR ([Category_sort] = @CategorySort AND @CategorySort is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Simcard_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Simcard_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Simcard_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Products_Simcard table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Simcard_Get_List

AS


				
				SELECT
					[Sim_Id],
					[Sim_Number],
					[Sim_Price],
					[Type_Id],
					[Type_Name],
					[IsActive]
				FROM
					[dbo].[Products_Simcard]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Simcard_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Simcard_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Simcard_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Products_Simcard table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Simcard_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Sim_Id] smallint 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Sim_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Sim_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Simcard]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Sim_Id], O.[Sim_Number], O.[Sim_Price], O.[Type_Id], O.[Type_Name], O.[IsActive]
				FROM
				    [dbo].[Products_Simcard] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Sim_Id] = PageIndex.[Sim_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Simcard]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Simcard_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Simcard_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Simcard_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Products_Simcard table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Simcard_Insert
(

	@SimId smallint    OUTPUT,

	@SimNumber nchar (15)  ,

	@SimPrice int   ,

	@TypeId int   ,

	@TypeName nvarchar (50)  ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[Products_Simcard]
					(
					[Sim_Number]
					,[Sim_Price]
					,[Type_Id]
					,[Type_Name]
					,[IsActive]
					)
				VALUES
					(
					@SimNumber
					,@SimPrice
					,@TypeId
					,@TypeName
					,@IsActive
					)
				
				-- Get the identity value
				SET @SimId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Simcard_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Simcard_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Simcard_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Products_Simcard table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Simcard_Update
(

	@SimId smallint   ,

	@SimNumber nchar (15)  ,

	@SimPrice int   ,

	@TypeId int   ,

	@TypeName nvarchar (50)  ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Products_Simcard]
				SET
					[Sim_Number] = @SimNumber
					,[Sim_Price] = @SimPrice
					,[Type_Id] = @TypeId
					,[Type_Name] = @TypeName
					,[IsActive] = @IsActive
				WHERE
[Sim_Id] = @SimId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Simcard_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Simcard_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Simcard_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Products_Simcard table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Simcard_Delete
(

	@SimId smallint   
)
AS


				DELETE FROM [dbo].[Products_Simcard] WITH (ROWLOCK) 
				WHERE
					[Sim_Id] = @SimId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Simcard_GetByTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Simcard_GetByTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Simcard_GetByTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Products_Simcard table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Simcard_GetByTypeId
(

	@TypeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[Sim_Id],
					[Sim_Number],
					[Sim_Price],
					[Type_Id],
					[Type_Name],
					[IsActive]
				FROM
					[dbo].[Products_Simcard]
				WHERE
					[Type_Id] = @TypeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Simcard_GetBySimId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Simcard_GetBySimId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Simcard_GetBySimId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Products_Simcard table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Simcard_GetBySimId
(

	@SimId smallint   
)
AS


				SELECT
					[Sim_Id],
					[Sim_Number],
					[Sim_Price],
					[Type_Id],
					[Type_Name],
					[IsActive]
				FROM
					[dbo].[Products_Simcard]
				WHERE
					[Sim_Id] = @SimId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Simcard_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Simcard_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Simcard_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Products_Simcard table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Simcard_Find
(

	@SearchUsingOR bit   = null ,

	@SimId smallint   = null ,

	@SimNumber nchar (15)  = null ,

	@SimPrice int   = null ,

	@TypeId int   = null ,

	@TypeName nvarchar (50)  = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Sim_Id]
	, [Sim_Number]
	, [Sim_Price]
	, [Type_Id]
	, [Type_Name]
	, [IsActive]
    FROM
	[dbo].[Products_Simcard]
    WHERE 
	 ([Sim_Id] = @SimId OR @SimId IS NULL)
	AND ([Sim_Number] = @SimNumber OR @SimNumber IS NULL)
	AND ([Sim_Price] = @SimPrice OR @SimPrice IS NULL)
	AND ([Type_Id] = @TypeId OR @TypeId IS NULL)
	AND ([Type_Name] = @TypeName OR @TypeName IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Sim_Id]
	, [Sim_Number]
	, [Sim_Price]
	, [Type_Id]
	, [Type_Name]
	, [IsActive]
    FROM
	[dbo].[Products_Simcard]
    WHERE 
	 ([Sim_Id] = @SimId AND @SimId is not null)
	OR ([Sim_Number] = @SimNumber AND @SimNumber is not null)
	OR ([Sim_Price] = @SimPrice AND @SimPrice is not null)
	OR ([Type_Id] = @TypeId AND @TypeId is not null)
	OR ([Type_Name] = @TypeName AND @TypeName is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Brand_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Brand_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Brand_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Products_Brand table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Brand_Get_List

AS


				
				SELECT
					[Brand_Id],
					[Brand_Name],
					[Brand_Title],
					[Brand_Image],
					[Brand_Sort],
					[IsActive],
					[Category_Id]
				FROM
					[dbo].[Products_Brand]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Brand_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Brand_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Brand_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Products_Brand table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Brand_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Brand_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Brand_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Brand_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Brand]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Brand_Id], O.[Brand_Name], O.[Brand_Title], O.[Brand_Image], O.[Brand_Sort], O.[IsActive], O.[Category_Id]
				FROM
				    [dbo].[Products_Brand] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Brand_Id] = PageIndex.[Brand_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Products_Brand]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Brand_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Brand_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Brand_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Products_Brand table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Brand_Insert
(

	@BrandId int    OUTPUT,

	@BrandName nvarchar (50)  ,

	@BrandTitle nvarchar (50)  ,

	@BrandImage nvarchar (50)  ,

	@BrandSort int   ,

	@IsActive bit   ,

	@CategoryId int   
)
AS


				
				INSERT INTO [dbo].[Products_Brand]
					(
					[Brand_Name]
					,[Brand_Title]
					,[Brand_Image]
					,[Brand_Sort]
					,[IsActive]
					,[Category_Id]
					)
				VALUES
					(
					@BrandName
					,@BrandTitle
					,@BrandImage
					,@BrandSort
					,@IsActive
					,@CategoryId
					)
				
				-- Get the identity value
				SET @BrandId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Brand_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Brand_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Brand_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Products_Brand table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Brand_Update
(

	@BrandId int   ,

	@BrandName nvarchar (50)  ,

	@BrandTitle nvarchar (50)  ,

	@BrandImage nvarchar (50)  ,

	@BrandSort int   ,

	@IsActive bit   ,

	@CategoryId int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Products_Brand]
				SET
					[Brand_Name] = @BrandName
					,[Brand_Title] = @BrandTitle
					,[Brand_Image] = @BrandImage
					,[Brand_Sort] = @BrandSort
					,[IsActive] = @IsActive
					,[Category_Id] = @CategoryId
				WHERE
[Brand_Id] = @BrandId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Brand_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Brand_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Brand_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Products_Brand table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Brand_Delete
(

	@BrandId int   
)
AS


				DELETE FROM [dbo].[Products_Brand] WITH (ROWLOCK) 
				WHERE
					[Brand_Id] = @BrandId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Brand_GetByBrandId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Brand_GetByBrandId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Brand_GetByBrandId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Products_Brand table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Brand_GetByBrandId
(

	@BrandId int   
)
AS


				SELECT
					[Brand_Id],
					[Brand_Name],
					[Brand_Title],
					[Brand_Image],
					[Brand_Sort],
					[IsActive],
					[Category_Id]
				FROM
					[dbo].[Products_Brand]
				WHERE
					[Brand_Id] = @BrandId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Products_Brand_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Products_Brand_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Products_Brand_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Products_Brand table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Products_Brand_Find
(

	@SearchUsingOR bit   = null ,

	@BrandId int   = null ,

	@BrandName nvarchar (50)  = null ,

	@BrandTitle nvarchar (50)  = null ,

	@BrandImage nvarchar (50)  = null ,

	@BrandSort int   = null ,

	@IsActive bit   = null ,

	@CategoryId int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Brand_Id]
	, [Brand_Name]
	, [Brand_Title]
	, [Brand_Image]
	, [Brand_Sort]
	, [IsActive]
	, [Category_Id]
    FROM
	[dbo].[Products_Brand]
    WHERE 
	 ([Brand_Id] = @BrandId OR @BrandId IS NULL)
	AND ([Brand_Name] = @BrandName OR @BrandName IS NULL)
	AND ([Brand_Title] = @BrandTitle OR @BrandTitle IS NULL)
	AND ([Brand_Image] = @BrandImage OR @BrandImage IS NULL)
	AND ([Brand_Sort] = @BrandSort OR @BrandSort IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
	AND ([Category_Id] = @CategoryId OR @CategoryId IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Brand_Id]
	, [Brand_Name]
	, [Brand_Title]
	, [Brand_Image]
	, [Brand_Sort]
	, [IsActive]
	, [Category_Id]
    FROM
	[dbo].[Products_Brand]
    WHERE 
	 ([Brand_Id] = @BrandId AND @BrandId is not null)
	OR ([Brand_Name] = @BrandName AND @BrandName is not null)
	OR ([Brand_Title] = @BrandTitle AND @BrandTitle is not null)
	OR ([Brand_Image] = @BrandImage AND @BrandImage is not null)
	OR ([Brand_Sort] = @BrandSort AND @BrandSort is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	OR ([Category_Id] = @CategoryId AND @CategoryId is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Image_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Image_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Image_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the News_Image table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Image_Get_List

AS


				
				SELECT
					[Image_Id],
					[Item_Id],
					[Lang_Id],
					[Image_URL],
					[Image_Description],
					[IsActive]
				FROM
					[dbo].[News_Image]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Image_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Image_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Image_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the News_Image table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Image_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Image_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Image_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Image_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[News_Image]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Image_Id], O.[Item_Id], O.[Lang_Id], O.[Image_URL], O.[Image_Description], O.[IsActive]
				FROM
				    [dbo].[News_Image] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Image_Id] = PageIndex.[Image_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[News_Image]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Image_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Image_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Image_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the News_Image table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Image_Insert
(

	@ImageId int    OUTPUT,

	@ItemId int   ,

	@LangId nchar (2)  ,

	@ImageUrl nvarchar (200)  ,

	@ImageDescription nvarchar (200)  ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[News_Image]
					(
					[Item_Id]
					,[Lang_Id]
					,[Image_URL]
					,[Image_Description]
					,[IsActive]
					)
				VALUES
					(
					@ItemId
					,@LangId
					,@ImageUrl
					,@ImageDescription
					,@IsActive
					)
				
				-- Get the identity value
				SET @ImageId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Image_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Image_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Image_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the News_Image table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Image_Update
(

	@ImageId int   ,

	@ItemId int   ,

	@LangId nchar (2)  ,

	@ImageUrl nvarchar (200)  ,

	@ImageDescription nvarchar (200)  ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[News_Image]
				SET
					[Item_Id] = @ItemId
					,[Lang_Id] = @LangId
					,[Image_URL] = @ImageUrl
					,[Image_Description] = @ImageDescription
					,[IsActive] = @IsActive
				WHERE
[Image_Id] = @ImageId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Image_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Image_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Image_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the News_Image table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Image_Delete
(

	@ImageId int   
)
AS


				DELETE FROM [dbo].[News_Image] WITH (ROWLOCK) 
				WHERE
					[Image_Id] = @ImageId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Image_GetByItemId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Image_GetByItemId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Image_GetByItemId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the News_Image table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Image_GetByItemId
(

	@ItemId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[Image_Id],
					[Item_Id],
					[Lang_Id],
					[Image_URL],
					[Image_Description],
					[IsActive]
				FROM
					[dbo].[News_Image]
				WHERE
					[Item_Id] = @ItemId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Image_GetByImageId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Image_GetByImageId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Image_GetByImageId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the News_Image table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Image_GetByImageId
(

	@ImageId int   
)
AS


				SELECT
					[Image_Id],
					[Item_Id],
					[Lang_Id],
					[Image_URL],
					[Image_Description],
					[IsActive]
				FROM
					[dbo].[News_Image]
				WHERE
					[Image_Id] = @ImageId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Image_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Image_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Image_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the News_Image table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Image_Find
(

	@SearchUsingOR bit   = null ,

	@ImageId int   = null ,

	@ItemId int   = null ,

	@LangId nchar (2)  = null ,

	@ImageUrl nvarchar (200)  = null ,

	@ImageDescription nvarchar (200)  = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Image_Id]
	, [Item_Id]
	, [Lang_Id]
	, [Image_URL]
	, [Image_Description]
	, [IsActive]
    FROM
	[dbo].[News_Image]
    WHERE 
	 ([Image_Id] = @ImageId OR @ImageId IS NULL)
	AND ([Item_Id] = @ItemId OR @ItemId IS NULL)
	AND ([Lang_Id] = @LangId OR @LangId IS NULL)
	AND ([Image_URL] = @ImageUrl OR @ImageUrl IS NULL)
	AND ([Image_Description] = @ImageDescription OR @ImageDescription IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Image_Id]
	, [Item_Id]
	, [Lang_Id]
	, [Image_URL]
	, [Image_Description]
	, [IsActive]
    FROM
	[dbo].[News_Image]
    WHERE 
	 ([Image_Id] = @ImageId AND @ImageId is not null)
	OR ([Item_Id] = @ItemId AND @ItemId is not null)
	OR ([Lang_Id] = @LangId AND @LangId is not null)
	OR ([Image_URL] = @ImageUrl AND @ImageUrl is not null)
	OR ([Image_Description] = @ImageDescription AND @ImageDescription is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Order_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Order_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Order_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Orders_Order table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Order_Get_List

AS


				
				SELECT
					[Order_Id],
					[Members_Id],
					[Order_Date],
					[Order_Quantity],
					[Order_Total],
					[Shipping_Name],
					[Shipping_Address],
					[Shipping_Tel],
					[Shipping_City],
					[Shipping_Notice],
					[IsActive]
				FROM
					[dbo].[Orders_Order]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Order_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Order_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Order_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Orders_Order table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Order_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Order_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Order_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Order_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Orders_Order]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Order_Id], O.[Members_Id], O.[Order_Date], O.[Order_Quantity], O.[Order_Total], O.[Shipping_Name], O.[Shipping_Address], O.[Shipping_Tel], O.[Shipping_City], O.[Shipping_Notice], O.[IsActive]
				FROM
				    [dbo].[Orders_Order] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Order_Id] = PageIndex.[Order_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Orders_Order]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Order_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Order_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Order_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Orders_Order table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Order_Insert
(

	@OrderId int    OUTPUT,

	@MembersId int   ,

	@OrderDate datetime   ,

	@OrderQuantity int   ,

	@OrderTotal float   ,

	@ShippingName nvarchar (50)  ,

	@ShippingAddress nvarchar (200)  ,

	@ShippingTel nchar (11)  ,

	@ShippingCity nvarchar (50)  ,

	@ShippingNotice nvarchar (500)  ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[Orders_Order]
					(
					[Members_Id]
					,[Order_Date]
					,[Order_Quantity]
					,[Order_Total]
					,[Shipping_Name]
					,[Shipping_Address]
					,[Shipping_Tel]
					,[Shipping_City]
					,[Shipping_Notice]
					,[IsActive]
					)
				VALUES
					(
					@MembersId
					,@OrderDate
					,@OrderQuantity
					,@OrderTotal
					,@ShippingName
					,@ShippingAddress
					,@ShippingTel
					,@ShippingCity
					,@ShippingNotice
					,@IsActive
					)
				
				-- Get the identity value
				SET @OrderId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Order_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Order_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Order_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Orders_Order table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Order_Update
(

	@OrderId int   ,

	@MembersId int   ,

	@OrderDate datetime   ,

	@OrderQuantity int   ,

	@OrderTotal float   ,

	@ShippingName nvarchar (50)  ,

	@ShippingAddress nvarchar (200)  ,

	@ShippingTel nchar (11)  ,

	@ShippingCity nvarchar (50)  ,

	@ShippingNotice nvarchar (500)  ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Orders_Order]
				SET
					[Members_Id] = @MembersId
					,[Order_Date] = @OrderDate
					,[Order_Quantity] = @OrderQuantity
					,[Order_Total] = @OrderTotal
					,[Shipping_Name] = @ShippingName
					,[Shipping_Address] = @ShippingAddress
					,[Shipping_Tel] = @ShippingTel
					,[Shipping_City] = @ShippingCity
					,[Shipping_Notice] = @ShippingNotice
					,[IsActive] = @IsActive
				WHERE
[Order_Id] = @OrderId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Order_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Order_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Order_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Orders_Order table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Order_Delete
(

	@OrderId int   
)
AS


				DELETE FROM [dbo].[Orders_Order] WITH (ROWLOCK) 
				WHERE
					[Order_Id] = @OrderId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Order_GetByMembersId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Order_GetByMembersId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Order_GetByMembersId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Orders_Order table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Order_GetByMembersId
(

	@MembersId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[Order_Id],
					[Members_Id],
					[Order_Date],
					[Order_Quantity],
					[Order_Total],
					[Shipping_Name],
					[Shipping_Address],
					[Shipping_Tel],
					[Shipping_City],
					[Shipping_Notice],
					[IsActive]
				FROM
					[dbo].[Orders_Order]
				WHERE
					[Members_Id] = @MembersId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Order_GetByOrderId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Order_GetByOrderId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Order_GetByOrderId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Orders_Order table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Order_GetByOrderId
(

	@OrderId int   
)
AS


				SELECT
					[Order_Id],
					[Members_Id],
					[Order_Date],
					[Order_Quantity],
					[Order_Total],
					[Shipping_Name],
					[Shipping_Address],
					[Shipping_Tel],
					[Shipping_City],
					[Shipping_Notice],
					[IsActive]
				FROM
					[dbo].[Orders_Order]
				WHERE
					[Order_Id] = @OrderId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Order_GetByProductsIdFromOrdersDetails procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Order_GetByProductsIdFromOrdersDetails') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Order_GetByProductsIdFromOrdersDetails
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records through a junction table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Order_GetByProductsIdFromOrdersDetails
(

	@ProductsId int   
)
AS


SELECT dbo.[Orders_Order].[Order_Id]
       ,dbo.[Orders_Order].[Members_Id]
       ,dbo.[Orders_Order].[Order_Date]
       ,dbo.[Orders_Order].[Order_Quantity]
       ,dbo.[Orders_Order].[Order_Total]
       ,dbo.[Orders_Order].[Shipping_Name]
       ,dbo.[Orders_Order].[Shipping_Address]
       ,dbo.[Orders_Order].[Shipping_Tel]
       ,dbo.[Orders_Order].[Shipping_City]
       ,dbo.[Orders_Order].[Shipping_Notice]
       ,dbo.[Orders_Order].[IsActive]
  FROM dbo.[Orders_Order]
 WHERE EXISTS (SELECT 1
                 FROM dbo.[Orders_Details] 
                WHERE dbo.[Orders_Details].[Products_Id] = @ProductsId
                  AND dbo.[Orders_Details].[Orders_Id] = dbo.[Orders_Order].[Order_Id]
                  )
				SELECT @@ROWCOUNT			
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Order_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Order_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Order_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Orders_Order table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Order_Find
(

	@SearchUsingOR bit   = null ,

	@OrderId int   = null ,

	@MembersId int   = null ,

	@OrderDate datetime   = null ,

	@OrderQuantity int   = null ,

	@OrderTotal float   = null ,

	@ShippingName nvarchar (50)  = null ,

	@ShippingAddress nvarchar (200)  = null ,

	@ShippingTel nchar (11)  = null ,

	@ShippingCity nvarchar (50)  = null ,

	@ShippingNotice nvarchar (500)  = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Order_Id]
	, [Members_Id]
	, [Order_Date]
	, [Order_Quantity]
	, [Order_Total]
	, [Shipping_Name]
	, [Shipping_Address]
	, [Shipping_Tel]
	, [Shipping_City]
	, [Shipping_Notice]
	, [IsActive]
    FROM
	[dbo].[Orders_Order]
    WHERE 
	 ([Order_Id] = @OrderId OR @OrderId IS NULL)
	AND ([Members_Id] = @MembersId OR @MembersId IS NULL)
	AND ([Order_Date] = @OrderDate OR @OrderDate IS NULL)
	AND ([Order_Quantity] = @OrderQuantity OR @OrderQuantity IS NULL)
	AND ([Order_Total] = @OrderTotal OR @OrderTotal IS NULL)
	AND ([Shipping_Name] = @ShippingName OR @ShippingName IS NULL)
	AND ([Shipping_Address] = @ShippingAddress OR @ShippingAddress IS NULL)
	AND ([Shipping_Tel] = @ShippingTel OR @ShippingTel IS NULL)
	AND ([Shipping_City] = @ShippingCity OR @ShippingCity IS NULL)
	AND ([Shipping_Notice] = @ShippingNotice OR @ShippingNotice IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Order_Id]
	, [Members_Id]
	, [Order_Date]
	, [Order_Quantity]
	, [Order_Total]
	, [Shipping_Name]
	, [Shipping_Address]
	, [Shipping_Tel]
	, [Shipping_City]
	, [Shipping_Notice]
	, [IsActive]
    FROM
	[dbo].[Orders_Order]
    WHERE 
	 ([Order_Id] = @OrderId AND @OrderId is not null)
	OR ([Members_Id] = @MembersId AND @MembersId is not null)
	OR ([Order_Date] = @OrderDate AND @OrderDate is not null)
	OR ([Order_Quantity] = @OrderQuantity AND @OrderQuantity is not null)
	OR ([Order_Total] = @OrderTotal AND @OrderTotal is not null)
	OR ([Shipping_Name] = @ShippingName AND @ShippingName is not null)
	OR ([Shipping_Address] = @ShippingAddress AND @ShippingAddress is not null)
	OR ([Shipping_Tel] = @ShippingTel AND @ShippingTel is not null)
	OR ([Shipping_City] = @ShippingCity AND @ShippingCity is not null)
	OR ([Shipping_Notice] = @ShippingNotice AND @ShippingNotice is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Item_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Item_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Item_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the News_Item table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Item_Get_List

AS


				
				SELECT
					[Item_Id],
					[Lang_id],
					[Item_Name],
					[Item_Title],
					[Item_Summary],
					[Item_Details],
					[Item_Author],
					[Item_PostDate],
					[Item_PostString],
					[Item_Image],
					[Item_ImageDescription],
					[Item_Keyword],
					[Item_View],
					[Category_Id],
					[Category_Title],
					[Category_Name],
					[IsHot],
					[IsActive]
				FROM
					[dbo].[News_Item]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Item_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Item_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Item_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the News_Item table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Item_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Item_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Item_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Item_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[News_Item]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Item_Id], O.[Lang_id], O.[Item_Name], O.[Item_Title], O.[Item_Summary], O.[Item_Details], O.[Item_Author], O.[Item_PostDate], O.[Item_PostString], O.[Item_Image], O.[Item_ImageDescription], O.[Item_Keyword], O.[Item_View], O.[Category_Id], O.[Category_Title], O.[Category_Name], O.[IsHot], O.[IsActive]
				FROM
				    [dbo].[News_Item] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Item_Id] = PageIndex.[Item_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[News_Item]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Item_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Item_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Item_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the News_Item table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Item_Insert
(

	@ItemId int    OUTPUT,

	@LangId nchar (2)  ,

	@ItemName nvarchar (200)  ,

	@ItemTitle nvarchar (200)  ,

	@ItemSummary nvarchar (500)  ,

	@ItemDetails ntext   ,

	@ItemAuthor nvarchar (100)  ,

	@ItemPostDate smalldatetime   ,

	@ItemPostString nvarchar (50)  ,

	@ItemImage nvarchar (200)  ,

	@ItemImageDescription nvarchar (200)  ,

	@ItemKeyword nvarchar (300)  ,

	@ItemView int   ,

	@CategoryId tinyint   ,

	@CategoryTitle nvarchar (200)  ,

	@CategoryName nvarchar (100)  ,

	@IsHot bit   ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[News_Item]
					(
					[Lang_id]
					,[Item_Name]
					,[Item_Title]
					,[Item_Summary]
					,[Item_Details]
					,[Item_Author]
					,[Item_PostDate]
					,[Item_PostString]
					,[Item_Image]
					,[Item_ImageDescription]
					,[Item_Keyword]
					,[Item_View]
					,[Category_Id]
					,[Category_Title]
					,[Category_Name]
					,[IsHot]
					,[IsActive]
					)
				VALUES
					(
					@LangId
					,@ItemName
					,@ItemTitle
					,@ItemSummary
					,@ItemDetails
					,@ItemAuthor
					,@ItemPostDate
					,@ItemPostString
					,@ItemImage
					,@ItemImageDescription
					,@ItemKeyword
					,@ItemView
					,@CategoryId
					,@CategoryTitle
					,@CategoryName
					,@IsHot
					,@IsActive
					)
				
				-- Get the identity value
				SET @ItemId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Item_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Item_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Item_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the News_Item table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Item_Update
(

	@ItemId int   ,

	@LangId nchar (2)  ,

	@ItemName nvarchar (200)  ,

	@ItemTitle nvarchar (200)  ,

	@ItemSummary nvarchar (500)  ,

	@ItemDetails ntext   ,

	@ItemAuthor nvarchar (100)  ,

	@ItemPostDate smalldatetime   ,

	@ItemPostString nvarchar (50)  ,

	@ItemImage nvarchar (200)  ,

	@ItemImageDescription nvarchar (200)  ,

	@ItemKeyword nvarchar (300)  ,

	@ItemView int   ,

	@CategoryId tinyint   ,

	@CategoryTitle nvarchar (200)  ,

	@CategoryName nvarchar (100)  ,

	@IsHot bit   ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[News_Item]
				SET
					[Lang_id] = @LangId
					,[Item_Name] = @ItemName
					,[Item_Title] = @ItemTitle
					,[Item_Summary] = @ItemSummary
					,[Item_Details] = @ItemDetails
					,[Item_Author] = @ItemAuthor
					,[Item_PostDate] = @ItemPostDate
					,[Item_PostString] = @ItemPostString
					,[Item_Image] = @ItemImage
					,[Item_ImageDescription] = @ItemImageDescription
					,[Item_Keyword] = @ItemKeyword
					,[Item_View] = @ItemView
					,[Category_Id] = @CategoryId
					,[Category_Title] = @CategoryTitle
					,[Category_Name] = @CategoryName
					,[IsHot] = @IsHot
					,[IsActive] = @IsActive
				WHERE
[Item_Id] = @ItemId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Item_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Item_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Item_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the News_Item table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Item_Delete
(

	@ItemId int   
)
AS


				DELETE FROM [dbo].[News_Item] WITH (ROWLOCK) 
				WHERE
					[Item_Id] = @ItemId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Item_GetByItemId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Item_GetByItemId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Item_GetByItemId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the News_Item table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Item_GetByItemId
(

	@ItemId int   
)
AS


				SELECT
					[Item_Id],
					[Lang_id],
					[Item_Name],
					[Item_Title],
					[Item_Summary],
					[Item_Details],
					[Item_Author],
					[Item_PostDate],
					[Item_PostString],
					[Item_Image],
					[Item_ImageDescription],
					[Item_Keyword],
					[Item_View],
					[Category_Id],
					[Category_Title],
					[Category_Name],
					[IsHot],
					[IsActive]
				FROM
					[dbo].[News_Item]
				WHERE
					[Item_Id] = @ItemId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Item_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Item_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Item_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the News_Item table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Item_Find
(

	@SearchUsingOR bit   = null ,

	@ItemId int   = null ,

	@LangId nchar (2)  = null ,

	@ItemName nvarchar (200)  = null ,

	@ItemTitle nvarchar (200)  = null ,

	@ItemSummary nvarchar (500)  = null ,

	@ItemDetails ntext   = null ,

	@ItemAuthor nvarchar (100)  = null ,

	@ItemPostDate smalldatetime   = null ,

	@ItemPostString nvarchar (50)  = null ,

	@ItemImage nvarchar (200)  = null ,

	@ItemImageDescription nvarchar (200)  = null ,

	@ItemKeyword nvarchar (300)  = null ,

	@ItemView int   = null ,

	@CategoryId tinyint   = null ,

	@CategoryTitle nvarchar (200)  = null ,

	@CategoryName nvarchar (100)  = null ,

	@IsHot bit   = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Item_Id]
	, [Lang_id]
	, [Item_Name]
	, [Item_Title]
	, [Item_Summary]
	, [Item_Details]
	, [Item_Author]
	, [Item_PostDate]
	, [Item_PostString]
	, [Item_Image]
	, [Item_ImageDescription]
	, [Item_Keyword]
	, [Item_View]
	, [Category_Id]
	, [Category_Title]
	, [Category_Name]
	, [IsHot]
	, [IsActive]
    FROM
	[dbo].[News_Item]
    WHERE 
	 ([Item_Id] = @ItemId OR @ItemId IS NULL)
	AND ([Lang_id] = @LangId OR @LangId IS NULL)
	AND ([Item_Name] = @ItemName OR @ItemName IS NULL)
	AND ([Item_Title] = @ItemTitle OR @ItemTitle IS NULL)
	AND ([Item_Summary] = @ItemSummary OR @ItemSummary IS NULL)
	AND ([Item_Author] = @ItemAuthor OR @ItemAuthor IS NULL)
	AND ([Item_PostDate] = @ItemPostDate OR @ItemPostDate IS NULL)
	AND ([Item_PostString] = @ItemPostString OR @ItemPostString IS NULL)
	AND ([Item_Image] = @ItemImage OR @ItemImage IS NULL)
	AND ([Item_ImageDescription] = @ItemImageDescription OR @ItemImageDescription IS NULL)
	AND ([Item_Keyword] = @ItemKeyword OR @ItemKeyword IS NULL)
	AND ([Item_View] = @ItemView OR @ItemView IS NULL)
	AND ([Category_Id] = @CategoryId OR @CategoryId IS NULL)
	AND ([Category_Title] = @CategoryTitle OR @CategoryTitle IS NULL)
	AND ([Category_Name] = @CategoryName OR @CategoryName IS NULL)
	AND ([IsHot] = @IsHot OR @IsHot IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Item_Id]
	, [Lang_id]
	, [Item_Name]
	, [Item_Title]
	, [Item_Summary]
	, [Item_Details]
	, [Item_Author]
	, [Item_PostDate]
	, [Item_PostString]
	, [Item_Image]
	, [Item_ImageDescription]
	, [Item_Keyword]
	, [Item_View]
	, [Category_Id]
	, [Category_Title]
	, [Category_Name]
	, [IsHot]
	, [IsActive]
    FROM
	[dbo].[News_Item]
    WHERE 
	 ([Item_Id] = @ItemId AND @ItemId is not null)
	OR ([Lang_id] = @LangId AND @LangId is not null)
	OR ([Item_Name] = @ItemName AND @ItemName is not null)
	OR ([Item_Title] = @ItemTitle AND @ItemTitle is not null)
	OR ([Item_Summary] = @ItemSummary AND @ItemSummary is not null)
	OR ([Item_Author] = @ItemAuthor AND @ItemAuthor is not null)
	OR ([Item_PostDate] = @ItemPostDate AND @ItemPostDate is not null)
	OR ([Item_PostString] = @ItemPostString AND @ItemPostString is not null)
	OR ([Item_Image] = @ItemImage AND @ItemImage is not null)
	OR ([Item_ImageDescription] = @ItemImageDescription AND @ItemImageDescription is not null)
	OR ([Item_Keyword] = @ItemKeyword AND @ItemKeyword is not null)
	OR ([Item_View] = @ItemView AND @ItemView is not null)
	OR ([Category_Id] = @CategoryId AND @CategoryId is not null)
	OR ([Category_Title] = @CategoryTitle AND @CategoryTitle is not null)
	OR ([Category_Name] = @CategoryName AND @CategoryName is not null)
	OR ([IsHot] = @IsHot AND @IsHot is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Category_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Category_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Category_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the News_Category table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Category_Get_List

AS


				
				SELECT
					[Category_Id],
					[Lang_Id],
					[Category_Name],
					[Category_Title],
					[Category_Image],
					[Category_ImageDescription],
					[Category_Summary],
					[Category_Keyword],
					[OrderKey],
					[IsActive]
				FROM
					[dbo].[News_Category]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Category_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Category_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Category_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the News_Category table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Category_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Category_Id] tinyint 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Category_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Category_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[News_Category]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Category_Id], O.[Lang_Id], O.[Category_Name], O.[Category_Title], O.[Category_Image], O.[Category_ImageDescription], O.[Category_Summary], O.[Category_Keyword], O.[OrderKey], O.[IsActive]
				FROM
				    [dbo].[News_Category] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Category_Id] = PageIndex.[Category_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[News_Category]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Category_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Category_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Category_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the News_Category table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Category_Insert
(

	@CategoryId tinyint    OUTPUT,

	@LangId nchar (2)  ,

	@CategoryName nvarchar (200)  ,

	@CategoryTitle nvarchar (200)  ,

	@CategoryImage nvarchar (200)  ,

	@CategoryImageDescription nvarchar (200)  ,

	@CategorySummary nvarchar (500)  ,

	@CategoryKeyword nvarchar (300)  ,

	@OrderKey tinyint   ,

	@IsActive bit   
)
AS


				
				INSERT INTO [dbo].[News_Category]
					(
					[Lang_Id]
					,[Category_Name]
					,[Category_Title]
					,[Category_Image]
					,[Category_ImageDescription]
					,[Category_Summary]
					,[Category_Keyword]
					,[OrderKey]
					,[IsActive]
					)
				VALUES
					(
					@LangId
					,@CategoryName
					,@CategoryTitle
					,@CategoryImage
					,@CategoryImageDescription
					,@CategorySummary
					,@CategoryKeyword
					,@OrderKey
					,@IsActive
					)
				
				-- Get the identity value
				SET @CategoryId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Category_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Category_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Category_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the News_Category table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Category_Update
(

	@CategoryId tinyint   ,

	@LangId nchar (2)  ,

	@CategoryName nvarchar (200)  ,

	@CategoryTitle nvarchar (200)  ,

	@CategoryImage nvarchar (200)  ,

	@CategoryImageDescription nvarchar (200)  ,

	@CategorySummary nvarchar (500)  ,

	@CategoryKeyword nvarchar (300)  ,

	@OrderKey tinyint   ,

	@IsActive bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[News_Category]
				SET
					[Lang_Id] = @LangId
					,[Category_Name] = @CategoryName
					,[Category_Title] = @CategoryTitle
					,[Category_Image] = @CategoryImage
					,[Category_ImageDescription] = @CategoryImageDescription
					,[Category_Summary] = @CategorySummary
					,[Category_Keyword] = @CategoryKeyword
					,[OrderKey] = @OrderKey
					,[IsActive] = @IsActive
				WHERE
[Category_Id] = @CategoryId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Category_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Category_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Category_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the News_Category table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Category_Delete
(

	@CategoryId tinyint   
)
AS


				DELETE FROM [dbo].[News_Category] WITH (ROWLOCK) 
				WHERE
					[Category_Id] = @CategoryId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Category_GetByCategoryId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Category_GetByCategoryId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Category_GetByCategoryId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the News_Category table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Category_GetByCategoryId
(

	@CategoryId tinyint   
)
AS


				SELECT
					[Category_Id],
					[Lang_Id],
					[Category_Name],
					[Category_Title],
					[Category_Image],
					[Category_ImageDescription],
					[Category_Summary],
					[Category_Keyword],
					[OrderKey],
					[IsActive]
				FROM
					[dbo].[News_Category]
				WHERE
					[Category_Id] = @CategoryId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.News_Category_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.News_Category_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.News_Category_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the News_Category table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.News_Category_Find
(

	@SearchUsingOR bit   = null ,

	@CategoryId tinyint   = null ,

	@LangId nchar (2)  = null ,

	@CategoryName nvarchar (200)  = null ,

	@CategoryTitle nvarchar (200)  = null ,

	@CategoryImage nvarchar (200)  = null ,

	@CategoryImageDescription nvarchar (200)  = null ,

	@CategorySummary nvarchar (500)  = null ,

	@CategoryKeyword nvarchar (300)  = null ,

	@OrderKey tinyint   = null ,

	@IsActive bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Category_Id]
	, [Lang_Id]
	, [Category_Name]
	, [Category_Title]
	, [Category_Image]
	, [Category_ImageDescription]
	, [Category_Summary]
	, [Category_Keyword]
	, [OrderKey]
	, [IsActive]
    FROM
	[dbo].[News_Category]
    WHERE 
	 ([Category_Id] = @CategoryId OR @CategoryId IS NULL)
	AND ([Lang_Id] = @LangId OR @LangId IS NULL)
	AND ([Category_Name] = @CategoryName OR @CategoryName IS NULL)
	AND ([Category_Title] = @CategoryTitle OR @CategoryTitle IS NULL)
	AND ([Category_Image] = @CategoryImage OR @CategoryImage IS NULL)
	AND ([Category_ImageDescription] = @CategoryImageDescription OR @CategoryImageDescription IS NULL)
	AND ([Category_Summary] = @CategorySummary OR @CategorySummary IS NULL)
	AND ([Category_Keyword] = @CategoryKeyword OR @CategoryKeyword IS NULL)
	AND ([OrderKey] = @OrderKey OR @OrderKey IS NULL)
	AND ([IsActive] = @IsActive OR @IsActive IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Category_Id]
	, [Lang_Id]
	, [Category_Name]
	, [Category_Title]
	, [Category_Image]
	, [Category_ImageDescription]
	, [Category_Summary]
	, [Category_Keyword]
	, [OrderKey]
	, [IsActive]
    FROM
	[dbo].[News_Category]
    WHERE 
	 ([Category_Id] = @CategoryId AND @CategoryId is not null)
	OR ([Lang_Id] = @LangId AND @LangId is not null)
	OR ([Category_Name] = @CategoryName AND @CategoryName is not null)
	OR ([Category_Title] = @CategoryTitle AND @CategoryTitle is not null)
	OR ([Category_Image] = @CategoryImage AND @CategoryImage is not null)
	OR ([Category_ImageDescription] = @CategoryImageDescription AND @CategoryImageDescription is not null)
	OR ([Category_Summary] = @CategorySummary AND @CategorySummary is not null)
	OR ([Category_Keyword] = @CategoryKeyword AND @CategoryKeyword is not null)
	OR ([OrderKey] = @OrderKey AND @OrderKey is not null)
	OR ([IsActive] = @IsActive AND @IsActive is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Details_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Details_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Details_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Orders_Details table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Details_Get_List

AS


				
				SELECT
					[Orders_Id],
					[Products_Id],
					[Products_Name],
					[Quantity],
					[Price],
					[Unit]
				FROM
					[dbo].[Orders_Details]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Details_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Details_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Details_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Orders_Details table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Details_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Orders_Id] int, [Products_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Orders_Id], [Products_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Orders_Id], [Products_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Orders_Details]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Orders_Id], O.[Products_Id], O.[Products_Name], O.[Quantity], O.[Price], O.[Unit]
				FROM
				    [dbo].[Orders_Details] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Orders_Id] = PageIndex.[Orders_Id]
					AND O.[Products_Id] = PageIndex.[Products_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Orders_Details]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Details_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Details_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Details_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Orders_Details table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Details_Insert
(

	@OrdersId int   ,

	@ProductsId int   ,

	@ProductsName nvarchar (50)  ,

	@Quantity int   ,

	@Price int   ,

	@Unit nvarchar (50)  
)
AS


				
				INSERT INTO [dbo].[Orders_Details]
					(
					[Orders_Id]
					,[Products_Id]
					,[Products_Name]
					,[Quantity]
					,[Price]
					,[Unit]
					)
				VALUES
					(
					@OrdersId
					,@ProductsId
					,@ProductsName
					,@Quantity
					,@Price
					,@Unit
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Details_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Details_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Details_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Orders_Details table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Details_Update
(

	@OrdersId int   ,

	@OriginalOrdersId int   ,

	@ProductsId int   ,

	@OriginalProductsId int   ,

	@ProductsName nvarchar (50)  ,

	@Quantity int   ,

	@Price int   ,

	@Unit nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Orders_Details]
				SET
					[Orders_Id] = @OrdersId
					,[Products_Id] = @ProductsId
					,[Products_Name] = @ProductsName
					,[Quantity] = @Quantity
					,[Price] = @Price
					,[Unit] = @Unit
				WHERE
[Orders_Id] = @OriginalOrdersId 
AND [Products_Id] = @OriginalProductsId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Details_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Details_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Details_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Orders_Details table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Details_Delete
(

	@OrdersId int   ,

	@ProductsId int   
)
AS


				DELETE FROM [dbo].[Orders_Details] WITH (ROWLOCK) 
				WHERE
					[Orders_Id] = @OrdersId
					AND [Products_Id] = @ProductsId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Details_GetByOrdersId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Details_GetByOrdersId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Details_GetByOrdersId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Orders_Details table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Details_GetByOrdersId
(

	@OrdersId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[Orders_Id],
					[Products_Id],
					[Products_Name],
					[Quantity],
					[Price],
					[Unit]
				FROM
					[dbo].[Orders_Details]
				WHERE
					[Orders_Id] = @OrdersId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Details_GetByProductsId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Details_GetByProductsId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Details_GetByProductsId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Orders_Details table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Details_GetByProductsId
(

	@ProductsId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[Orders_Id],
					[Products_Id],
					[Products_Name],
					[Quantity],
					[Price],
					[Unit]
				FROM
					[dbo].[Orders_Details]
				WHERE
					[Products_Id] = @ProductsId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Details_GetByOrdersIdProductsId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Details_GetByOrdersIdProductsId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Details_GetByOrdersIdProductsId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Orders_Details table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Details_GetByOrdersIdProductsId
(

	@OrdersId int   ,

	@ProductsId int   
)
AS


				SELECT
					[Orders_Id],
					[Products_Id],
					[Products_Name],
					[Quantity],
					[Price],
					[Unit]
				FROM
					[dbo].[Orders_Details]
				WHERE
					[Orders_Id] = @OrdersId
					AND [Products_Id] = @ProductsId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Details_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Details_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Details_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Orders_Details table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Details_Find
(

	@SearchUsingOR bit   = null ,

	@OrdersId int   = null ,

	@ProductsId int   = null ,

	@ProductsName nvarchar (50)  = null ,

	@Quantity int   = null ,

	@Price int   = null ,

	@Unit nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Orders_Id]
	, [Products_Id]
	, [Products_Name]
	, [Quantity]
	, [Price]
	, [Unit]
    FROM
	[dbo].[Orders_Details]
    WHERE 
	 ([Orders_Id] = @OrdersId OR @OrdersId IS NULL)
	AND ([Products_Id] = @ProductsId OR @ProductsId IS NULL)
	AND ([Products_Name] = @ProductsName OR @ProductsName IS NULL)
	AND ([Quantity] = @Quantity OR @Quantity IS NULL)
	AND ([Price] = @Price OR @Price IS NULL)
	AND ([Unit] = @Unit OR @Unit IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Orders_Id]
	, [Products_Id]
	, [Products_Name]
	, [Quantity]
	, [Price]
	, [Unit]
    FROM
	[dbo].[Orders_Details]
    WHERE 
	 ([Orders_Id] = @OrdersId AND @OrdersId is not null)
	OR ([Products_Id] = @ProductsId AND @ProductsId is not null)
	OR ([Products_Name] = @ProductsName AND @ProductsName is not null)
	OR ([Quantity] = @Quantity AND @Quantity is not null)
	OR ([Price] = @Price AND @Price is not null)
	OR ([Unit] = @Unit AND @Unit is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_Import_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_Import_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_Import_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Order_Import table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_Import_Get_List

AS


				
				SELECT
					[Order_Id],
					[Order_Date],
					[Supply_Id],
					[People_Name],
					[Quantity],
					[Toltal],
					[Notice]
				FROM
					[dbo].[Order_Import]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_Import_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_Import_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_Import_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Order_Import table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_Import_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Order_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Order_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Order_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Order_Import]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Order_Id], O.[Order_Date], O.[Supply_Id], O.[People_Name], O.[Quantity], O.[Toltal], O.[Notice]
				FROM
				    [dbo].[Order_Import] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Order_Id] = PageIndex.[Order_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Order_Import]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_Import_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_Import_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_Import_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Order_Import table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_Import_Insert
(

	@OrderId int    OUTPUT,

	@OrderDate datetime   ,

	@SupplyId int   ,

	@PeopleName nvarchar (50)  ,

	@Quantity int   ,

	@Toltal int   ,

	@Notice nvarchar (250)  
)
AS


				
				INSERT INTO [dbo].[Order_Import]
					(
					[Order_Date]
					,[Supply_Id]
					,[People_Name]
					,[Quantity]
					,[Toltal]
					,[Notice]
					)
				VALUES
					(
					@OrderDate
					,@SupplyId
					,@PeopleName
					,@Quantity
					,@Toltal
					,@Notice
					)
				
				-- Get the identity value
				SET @OrderId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_Import_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_Import_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_Import_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Order_Import table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_Import_Update
(

	@OrderId int   ,

	@OrderDate datetime   ,

	@SupplyId int   ,

	@PeopleName nvarchar (50)  ,

	@Quantity int   ,

	@Toltal int   ,

	@Notice nvarchar (250)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Order_Import]
				SET
					[Order_Date] = @OrderDate
					,[Supply_Id] = @SupplyId
					,[People_Name] = @PeopleName
					,[Quantity] = @Quantity
					,[Toltal] = @Toltal
					,[Notice] = @Notice
				WHERE
[Order_Id] = @OrderId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_Import_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_Import_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_Import_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Order_Import table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_Import_Delete
(

	@OrderId int   
)
AS


				DELETE FROM [dbo].[Order_Import] WITH (ROWLOCK) 
				WHERE
					[Order_Id] = @OrderId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_Import_GetByOrderId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_Import_GetByOrderId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_Import_GetByOrderId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Order_Import table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_Import_GetByOrderId
(

	@OrderId int   
)
AS


				SELECT
					[Order_Id],
					[Order_Date],
					[Supply_Id],
					[People_Name],
					[Quantity],
					[Toltal],
					[Notice]
				FROM
					[dbo].[Order_Import]
				WHERE
					[Order_Id] = @OrderId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_Import_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_Import_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_Import_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Order_Import table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_Import_Find
(

	@SearchUsingOR bit   = null ,

	@OrderId int   = null ,

	@OrderDate datetime   = null ,

	@SupplyId int   = null ,

	@PeopleName nvarchar (50)  = null ,

	@Quantity int   = null ,

	@Toltal int   = null ,

	@Notice nvarchar (250)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Order_Id]
	, [Order_Date]
	, [Supply_Id]
	, [People_Name]
	, [Quantity]
	, [Toltal]
	, [Notice]
    FROM
	[dbo].[Order_Import]
    WHERE 
	 ([Order_Id] = @OrderId OR @OrderId IS NULL)
	AND ([Order_Date] = @OrderDate OR @OrderDate IS NULL)
	AND ([Supply_Id] = @SupplyId OR @SupplyId IS NULL)
	AND ([People_Name] = @PeopleName OR @PeopleName IS NULL)
	AND ([Quantity] = @Quantity OR @Quantity IS NULL)
	AND ([Toltal] = @Toltal OR @Toltal IS NULL)
	AND ([Notice] = @Notice OR @Notice IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Order_Id]
	, [Order_Date]
	, [Supply_Id]
	, [People_Name]
	, [Quantity]
	, [Toltal]
	, [Notice]
    FROM
	[dbo].[Order_Import]
    WHERE 
	 ([Order_Id] = @OrderId AND @OrderId is not null)
	OR ([Order_Date] = @OrderDate AND @OrderDate is not null)
	OR ([Supply_Id] = @SupplyId AND @SupplyId is not null)
	OR ([People_Name] = @PeopleName AND @PeopleName is not null)
	OR ([Quantity] = @Quantity AND @Quantity is not null)
	OR ([Toltal] = @Toltal AND @Toltal is not null)
	OR ([Notice] = @Notice AND @Notice is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUserLog_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUserLog_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUserLog_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the TblUserLog table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUserLog_Get_List

AS


				
				SELECT
					[Log_Id],
					[User_UserName],
					[LogTime],
					[IPAddress],
					[Log_Operator]
				FROM
					[dbo].[TblUserLog]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUserLog_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUserLog_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUserLog_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the TblUserLog table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUserLog_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Log_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Log_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Log_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[TblUserLog]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Log_Id], O.[User_UserName], O.[LogTime], O.[IPAddress], O.[Log_Operator]
				FROM
				    [dbo].[TblUserLog] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Log_Id] = PageIndex.[Log_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[TblUserLog]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUserLog_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUserLog_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUserLog_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the TblUserLog table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUserLog_Insert
(

	@LogId int    OUTPUT,

	@UserUserName nvarchar (50)  ,

	@LogTime nvarchar (50)  ,

	@IpAddress nchar (15)  ,

	@LogOperator nvarchar (200)  
)
AS


				
				INSERT INTO [dbo].[TblUserLog]
					(
					[User_UserName]
					,[LogTime]
					,[IPAddress]
					,[Log_Operator]
					)
				VALUES
					(
					@UserUserName
					,@LogTime
					,@IpAddress
					,@LogOperator
					)
				
				-- Get the identity value
				SET @LogId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUserLog_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUserLog_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUserLog_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the TblUserLog table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUserLog_Update
(

	@LogId int   ,

	@UserUserName nvarchar (50)  ,

	@LogTime nvarchar (50)  ,

	@IpAddress nchar (15)  ,

	@LogOperator nvarchar (200)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[TblUserLog]
				SET
					[User_UserName] = @UserUserName
					,[LogTime] = @LogTime
					,[IPAddress] = @IpAddress
					,[Log_Operator] = @LogOperator
				WHERE
[Log_Id] = @LogId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUserLog_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUserLog_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUserLog_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the TblUserLog table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUserLog_Delete
(

	@LogId int   
)
AS


				DELETE FROM [dbo].[TblUserLog] WITH (ROWLOCK) 
				WHERE
					[Log_Id] = @LogId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUserLog_GetByLogId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUserLog_GetByLogId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUserLog_GetByLogId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the TblUserLog table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUserLog_GetByLogId
(

	@LogId int   
)
AS


				SELECT
					[Log_Id],
					[User_UserName],
					[LogTime],
					[IPAddress],
					[Log_Operator]
				FROM
					[dbo].[TblUserLog]
				WHERE
					[Log_Id] = @LogId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TblUserLog_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TblUserLog_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TblUserLog_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the TblUserLog table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TblUserLog_Find
(

	@SearchUsingOR bit   = null ,

	@LogId int   = null ,

	@UserUserName nvarchar (50)  = null ,

	@LogTime nvarchar (50)  = null ,

	@IpAddress nchar (15)  = null ,

	@LogOperator nvarchar (200)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Log_Id]
	, [User_UserName]
	, [LogTime]
	, [IPAddress]
	, [Log_Operator]
    FROM
	[dbo].[TblUserLog]
    WHERE 
	 ([Log_Id] = @LogId OR @LogId IS NULL)
	AND ([User_UserName] = @UserUserName OR @UserUserName IS NULL)
	AND ([LogTime] = @LogTime OR @LogTime IS NULL)
	AND ([IPAddress] = @IpAddress OR @IpAddress IS NULL)
	AND ([Log_Operator] = @LogOperator OR @LogOperator IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Log_Id]
	, [User_UserName]
	, [LogTime]
	, [IPAddress]
	, [Log_Operator]
    FROM
	[dbo].[TblUserLog]
    WHERE 
	 ([Log_Id] = @LogId AND @LogId is not null)
	OR ([User_UserName] = @UserUserName AND @UserUserName is not null)
	OR ([LogTime] = @LogTime AND @LogTime is not null)
	OR ([IPAddress] = @IpAddress AND @IpAddress is not null)
	OR ([Log_Operator] = @LogOperator AND @LogOperator is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_ImportDetail_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_ImportDetail_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_ImportDetail_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the Order_ImportDetail table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_ImportDetail_Get_List

AS


				
				SELECT
					[Order_Id],
					[Products_Id],
					[Products_Name],
					[Quantity],
					[Price],
					[Total],
					[Notice]
				FROM
					[dbo].[Order_ImportDetail]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_ImportDetail_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_ImportDetail_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_ImportDetail_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the Order_ImportDetail table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_ImportDetail_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Order_Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Order_Id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [Order_Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Order_ImportDetail]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[Order_Id], O.[Products_Id], O.[Products_Name], O.[Quantity], O.[Price], O.[Total], O.[Notice]
				FROM
				    [dbo].[Order_ImportDetail] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Order_Id] = PageIndex.[Order_Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Order_ImportDetail]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_ImportDetail_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_ImportDetail_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_ImportDetail_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the Order_ImportDetail table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_ImportDetail_Insert
(

	@OrderId int   ,

	@ProductsId int   ,

	@ProductsName nvarchar (50)  ,

	@Quantity int   ,

	@Price int   ,

	@Total int   ,

	@Notice nvarchar (250)  
)
AS


				
				INSERT INTO [dbo].[Order_ImportDetail]
					(
					[Order_Id]
					,[Products_Id]
					,[Products_Name]
					,[Quantity]
					,[Price]
					,[Total]
					,[Notice]
					)
				VALUES
					(
					@OrderId
					,@ProductsId
					,@ProductsName
					,@Quantity
					,@Price
					,@Total
					,@Notice
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_ImportDetail_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_ImportDetail_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_ImportDetail_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the Order_ImportDetail table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_ImportDetail_Update
(

	@OrderId int   ,

	@OriginalOrderId int   ,

	@ProductsId int   ,

	@ProductsName nvarchar (50)  ,

	@Quantity int   ,

	@Price int   ,

	@Total int   ,

	@Notice nvarchar (250)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Order_ImportDetail]
				SET
					[Order_Id] = @OrderId
					,[Products_Id] = @ProductsId
					,[Products_Name] = @ProductsName
					,[Quantity] = @Quantity
					,[Price] = @Price
					,[Total] = @Total
					,[Notice] = @Notice
				WHERE
[Order_Id] = @OriginalOrderId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_ImportDetail_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_ImportDetail_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_ImportDetail_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the Order_ImportDetail table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_ImportDetail_Delete
(

	@OrderId int   
)
AS


				DELETE FROM [dbo].[Order_ImportDetail] WITH (ROWLOCK) 
				WHERE
					[Order_Id] = @OrderId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_ImportDetail_GetByOrderId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_ImportDetail_GetByOrderId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_ImportDetail_GetByOrderId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the Order_ImportDetail table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_ImportDetail_GetByOrderId
(

	@OrderId int   
)
AS


				SELECT
					[Order_Id],
					[Products_Id],
					[Products_Name],
					[Quantity],
					[Price],
					[Total],
					[Notice]
				FROM
					[dbo].[Order_ImportDetail]
				WHERE
					[Order_Id] = @OrderId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Order_ImportDetail_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Order_ImportDetail_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Order_ImportDetail_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Order_ImportDetail table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Order_ImportDetail_Find
(

	@SearchUsingOR bit   = null ,

	@OrderId int   = null ,

	@ProductsId int   = null ,

	@ProductsName nvarchar (50)  = null ,

	@Quantity int   = null ,

	@Price int   = null ,

	@Total int   = null ,

	@Notice nvarchar (250)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Order_Id]
	, [Products_Id]
	, [Products_Name]
	, [Quantity]
	, [Price]
	, [Total]
	, [Notice]
    FROM
	[dbo].[Order_ImportDetail]
    WHERE 
	 ([Order_Id] = @OrderId OR @OrderId IS NULL)
	AND ([Products_Id] = @ProductsId OR @ProductsId IS NULL)
	AND ([Products_Name] = @ProductsName OR @ProductsName IS NULL)
	AND ([Quantity] = @Quantity OR @Quantity IS NULL)
	AND ([Price] = @Price OR @Price IS NULL)
	AND ([Total] = @Total OR @Total IS NULL)
	AND ([Notice] = @Notice OR @Notice IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Order_Id]
	, [Products_Id]
	, [Products_Name]
	, [Quantity]
	, [Price]
	, [Total]
	, [Notice]
    FROM
	[dbo].[Order_ImportDetail]
    WHERE 
	 ([Order_Id] = @OrderId AND @OrderId is not null)
	OR ([Products_Id] = @ProductsId AND @ProductsId is not null)
	OR ([Products_Name] = @ProductsName AND @ProductsName is not null)
	OR ([Quantity] = @Quantity AND @Quantity is not null)
	OR ([Price] = @Price AND @Price is not null)
	OR ([Total] = @Total AND @Total is not null)
	OR ([Notice] = @Notice AND @Notice is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

